/**
 *
 * @param mun 限制浮点数2位
 */
export const numbTo = (mun: number | string | void): number => {
  if (!mun) return 0
  try {
    const arr: string[] = Number(mun)
      .toFixed(2)
      .toString()
      .split('.')
    if (arr[1] === '00') return Number(arr[0])
    return Number(`${arr[0]}.${arr[1]}`)
  } catch (e) {
    return 0
  }
}
/**
 *
 * @param num1求和
 * @param num2求和
 */
export const numAdd = (num1: number, num2: number): number => {
  let baseNum: number, baseNum1: number, baseNum2: number
  try {
    baseNum1 = num1.toString().split('.')[1].length
  } catch (e) {
    baseNum1 = 0
  }
  try {
    baseNum2 = num2.toString().split('.')[1].length
  } catch (e) {
    baseNum2 = 0
  }
  baseNum = Math.pow(10, Math.max(baseNum1, baseNum2))
  return (num1 * baseNum + num2 * baseNum) / baseNum
}
/**
 * @param num1被减数
 * @param num2减数
 */
export const numSub = (num1: number, num2: number): number => {
  let baseNum: number, baseNum1: number, baseNum2: number
  try {
    baseNum1 = num1.toString().split('.')[1].length
  } catch (e) {
    baseNum1 = 0
  }
  try {
    baseNum2 = num2.toString().split('.')[1].length
  } catch (e) {
    baseNum2 = 0
  }
  baseNum = Math.pow(10, Math.max(baseNum1, baseNum2))
  return (num1 * baseNum - num2 * baseNum) / baseNum
}
/**
 * @param num1被乘数
 * @param num2乘数
 */
export const numMulti = (num1: number, num2: number): number => {
  let baseNum: number = 0
  try {
    baseNum += num1.toString().split('.')[1].length
  } catch (e) {}
  try {
    baseNum += num2.toString().split('.')[1].length
  } catch (e) {}
  return (
    (Number(num1.toString().replace('.', '')) *
      Number(num2.toString().replace('.', ''))) /
    Math.pow(10, baseNum)
  )
}
/**
 * @param num1被除数
 * @param num2除数
 */
export const numDiv = (num1: number, num2: number): number => {
  let baseNum1: number = 0,
    baseNum2: number = 0
  let baseNum3: number, baseNum4: number
  try {
    baseNum1 = num1.toString().split('.')[1].length
  } catch (e) {
    baseNum1 = 0
  }
  try {
    baseNum2 = num2.toString().split('.')[1].length
  } catch (e) {
    baseNum2 = 0
  }
  baseNum3 = Number(num1.toString().replace('.', ''))
  baseNum4 = Number(num2.toString().replace('.', ''))
  return (baseNum3 / baseNum4) * Math.pow(10, baseNum2 - baseNum1)
}

/**
 *
 * @param data 列表
 * @param key 顺序规则
 * @param isReverse 默认小到大 为true大到小
 */
export const sortArrObj = (data, key, isReverse: boolean = false) => {
  // 编写方法，实现冒泡
  //外层循环，控制趟数，每一次找到一个最大值
  for (var i = 0; i < data.length - 1; i += 1) {
    // 内层循环,控制比较的次数，并且判断两个数的大小
    for (var j = 0; j < data.length - 1 - i; j += 1) {
      // 白话解释：如果前面的数大，放到后面(当然是从小到大的冒泡排序)
      if (+data[j][key] > +data[j + 1][key]) {
        var temp = data[j]
        data[j] = data[j + 1]
        data[j + 1] = temp
      }
    }
  }
  if (isReverse) {
    data = data.reverse()
  }
  return data
}
// ! js toFixed()方法不准确的解决方案 ( 在跟组件中引入并调用 )
// 参考文章 https://www.jianshu.com/p/cae048479372
//代码来源，https://www.cnblogs.com/gushen/archive/2012/11/20/2778324.html
export const fixToFixedIssue = () => {
  Number.prototype.toFixed = function(d) {
    var s = this + ''
    if (!d) d = 0
    if (s.indexOf('.') == -1) s += '.'
    s += new Array(d + 1).join('0')
    if (
      new RegExp('^(-|\\+)?(\\d+(\\.\\d{0,' + (d + 1) + '})?)\\d*$').test(s)
    ) {
      var s = '0' + RegExp.$2,
        pm: any = RegExp.$1,
        a: any = RegExp.$3.length,
        b: any = true
      if (a == d + 2) {
        a = s.match(/\d/g)
        if (parseInt(a[a.length - 1]) > 4) {
          for (var i = a.length - 2; i >= 0; i--) {
            a[i] = parseInt(a[i]) + 1
            if (a[i] == 10) {
              a[i] = 0
              b = i != 1
            } else break
          }
        }
        s = a
          .join('')
          .replace(new RegExp('(\\d+)(\\d{' + d + '})\\d$'), '$1.$2')
      }
      if (b) s = s.substr(1)
      return (pm + s).replace(/\.$/, '')
    }
    return this + ''
  }
}
